import request from '../../utils/request'

Page({
  data: {
    paymentList: [],
    amount: null,
    selectedMethod: 'alipay', 
    signId: null,
    servicePackageName: null
  },
  getpayment(){
    console.log('1111');
    request({
        url: 'package/package/list',
        method: 'GET',
        success: (res) => {
            console.log(res.data);
            console.log(res.data.rows);
            const { servicePackageName } = this.data;
            const targetPackage = res.data.rows.find(item => item.packageName === servicePackageName);
            if (targetPackage) {
                const price = targetPackage.price;
                this.setData({
                    amount: price
                });
            }
        },
        fail: (err) => {
            console.log('获取数据失败', err);
        }
    });
  },

  onLoad: function(options) {
    if (options.servicePackageName) {
      this.setData({
        servicePackageName: options.servicePackageName
      });
    }
    this.getpayment()
    // 从options中获取支付金额和签约ID
    if (options.amount) {
      this.setData({
        amount: options.amount
      });
    }
    if (options.signId) {
      this.setData({
        signId: options.signId
      });
    }
  },
 
  selectPayMethod: function(e) {
    const method = e.currentTarget.dataset.method;
    this.setData({
      selectedMethod: method
    });
  },

  handlePayment: function() {
    // 根据选择的支付方式处理支付
    wx.showLoading({
      title: '支付处理中...',
    });

    // 模拟支付过程
    setTimeout(() => {
      wx.hideLoading();
      
      // 确保有signId
      if (!this.data.signId) {
        wx.showToast({
          title: '签约ID不存在',
          icon: 'none'
        });
        return;
      }
      
      // 支付成功后跳转到支付结果页，并传递signId
      wx.redirectTo({
        url: '/pages/payment-result/payment-result?signId=${this.data.signId}'
      });
    }, 1500);
  }
});